package cn.ges.random;

import java.util.concurrent.ThreadLocalRandom;

/**
 * @author gespent@163.com
 * @date 2019/07/25 11:46
 */
public class RandomUtil {

    private static Sequence sequence = new Sequence();

    /**
     * 返回全局Sequence数字
     */
    public static long getSequence() {
        return sequence.nextId();
    }

    /**
     * 获得一个六位随机正整数
     */
    public static long get6RandomNumber() {
        return ThreadLocalRandom.current().nextLong(900000) + 100000;
    }

    /**
     * 根据最大最小值 获得一个随机数
     *
     * @apiNote max值为不包含 比如 1-2 只能随机到1-1
     */
    public static Long getNumberRandomValue(long min, long max) {
        if (min > max) {
            return null;
        }
        if (min == max) {
            return min;
        }
        return ThreadLocalRandom.current().nextLong(min, max);
    }

    public static Long getNumberRandomValue(Number min, Number max) {
        final long l = min.longValue();
        final long l2 = max.longValue();
        if (l > l2) {
            return null;
        }
        if (l == l2) {
            return l;
        }
        return ThreadLocalRandom.current().nextLong(l, l2);
    }

    public static boolean getProbabilityInOneHundred(int probability) {
        final Long numberRandomValue = RandomUtil.getNumberRandomValue(0, 100);
        if (numberRandomValue == null) {
            return false;
        }
        return probability >= numberRandomValue;
    }
}
